/**
 * basic javascript functionality needed on every page
 */

 /*
  * add lavalamp effect to main menu
  */
 initializeLavalamp = function () {
 	/* show hidden dummy menu elements, used when no menu element is selected */
 	$('#navigation .main li.first').css('display', 'block');
 	$('#navigation .main li.first').css('position', 'absolute');

 	$('#page .content .submenu li.first').css('display', 'block');
 	$('#page .content .submenu li.first').css('position', 'absolute');

	/* move dummy menu elements left out of viewable area */
 	if (isIE) {
 		var mainNavigationOffset = $('.main .weblog').offset();
    	var mainNavigationFirstLiOffset = mainNavigationOffset.left + 100;

    	$('#navigation .main li.first').css('left', '-' + mainNavigationFirstLiOffset + 'px');
 	} else {
 		$('#navigation .main li.first').css('left', '-100px');
	}

	if ($('#page .content .submenu li').length > 0) {
		var submenuOffset = $('#page .content .submenu li.first').next().offset();
		var submenuFirstLiOffset = submenuOffset.left + 180;

		$('#page .content .submenu li.first').css('left', '-' + submenuFirstLiOffset + 'px');
	}

	/* load lavalamp for main menu */
 	$('#navigation .main').lavaLamp({
            fx: 'linear',
            speed: 300
    });

    $('#navigation .main .back').append('<span class="right">&nbsp;</span>');
    $('#navigation .main .back').prepend('<span class="left">&nbsp;</span>');

	/* load lavalamp for sumbenu */
 	$('.content .submenu').lavaLamp({
            fx: 'linear',
            speed: 300
    });

    $('#page .content .submenu .back').html('<span class="middle">&nbsp;</span>');
    $('#page .content .submenu .back').append('<span class="right">&nbsp;</span>');
    $('#page .content .submenu .back').prepend('<span class="left">&nbsp;</span>');

	if (isIE) {
    	fixIElavalamp();
    }
}

/*
 * fixes lavalamp highlighting position on Internet Explorers
 */
fixIElavalamp = function () {
	var mainNavigationOffset = $('.main .weblog').offset();
    var mainNavigationFirstLiOffset = mainNavigationOffset.left + 100;

	if (browserVersion == 6) {
		mainNavigationOffset.left = mainNavigationOffset.left - 10;
		$('#page .content .submenu .back').css('left', '10px');
	}

	$('.main .back').css('margin-left', mainNavigationOffset.left);
}

/*
 * processes PNG files with Microsoft filter, only on Internet Explorer 6
 */
fixIE6alphaTransparency = function () {
	if (isIE && browserVersion == 6) {
		$('#browser_shadow').each(pngfix);
		$('h1 a').each(pngfix);
	    $('#navigation .main a').each(pngfix);
	    $('#page .content .submenu .back span.left').each(pngfix);
	    $('#page .content .submenu .back span.right').each(pngfix);
	}
}

/*
 * render headlines with sIFR flash fonts
 */
renderFlashHeadlines = function () {
	$('#page .content h2').css('height', '32px');
	$('#page .content h3').css('height', '26px');
	$('.teaser .content h3.teaser_headline').css('height', '24px');

	if (isIE) {
		$('#page .content h2').css('margin-top', '30px')
	}

    sIFR.setup();
    sIFR.replaceElement("h2", named({sFlashSrc: "/includes/flashfonts/yanone-kaffeesatz_regular.swf", sColor: "#046380", sBgColor: "transparent", sWmode: "transparent"}));
	sIFR.replaceElement("h3.teaser_headline", named({sFlashSrc: "/includes/flashfonts/yanone-kaffeesatz_regular.swf", sColor: "#ffffff", sBgColor: "transparent", sWmode: "transparent"}));
    sIFR.replaceElement("h3", named({sFlashSrc: "/includes/flashfonts/yanone-kaffeesatz_regular.swf", sColor: "#046380", sBgColor: "transparent", sWmode: "transparent"}));
}

/*
 * initializes lavalamp menus
 */
initializeMenus = function () {
	initializeLavalamp();

    $('#navigation .main a').removeClass('cssHover');
    $('#navigation .main a').removeClass('current');

    $('.content .submenu a').removeClass('cssHover');
    $('.content .submenu a').removeClass('current');

    $(window).resize(function () {
    	$('#navigation .main .back').remove();
    	$('.content .submenu .back').remove();
		initializeLavalamp();
    });
}


/*
 * render headlines with Flash but skip on Linux
 * since flash transparency won't work, except
 * if Linux is running Firefox 3 with Flashplayer 10+
 */
initializeHeadlines = function () {
	var requiredFlashPlayerMajor = 10;
	var requiredFlashPlayerMinor = 0;
	var requiredFlashPlayerRevision = 0;

	var FlashPlayerHasReqestedVersion = DetectFlashVer(requiredFlashPlayerMajor, requiredFlashPlayerMinor, requiredFlashPlayerRevision);

	if (!isLinux || (isFirefox3 && FlashPlayerHasReqestedVersion)) {
		if(typeof sIFR == "function"){
			renderFlashHeadlines();

		};
	}
}

/*
 *  initialize searchform AJAX behaviour
 */
initializeSearchform = function () {
	$('#quicksearch').bind('focus', quicksearchFocus);
	$('#quicksearch').bind('blur', quicksearchBlur);
	$('#quicksearch').bind('keyup', quicksearchTyping);

	$('#quicksearchform').submit(function () {
		return false;
	});

	var searchformOffset = $('#quicksearch').offset();
	var quickresultsOffset = searchformOffset.left -150;

	$('#quickresults').css('left', quickresultsOffset + 'px');
}

/*
 *  initialize jquery lightboxes
 */
initializeImageboxes = function () {
	setupZoom();
}

/*
 * animate to-top link with soft scrolling
 */
initializeToTopLink = function () {
	myScrollToTop = function () {
		$('#top').ScrollTo(1000);
		$('#pae .footer a').click(myScrollToTop);
	}

	$('#page .footer a').click(myScrollToTop);
	$('#page .footer a').removeAttr('href');
}

jQuery.dequeue = function () {
	return false;
}

/*
 * make title ribbon fixation possible by clicking it
 */
initializeRibbon = function () {
	changeRibbonPositon = function () {
		if ($('h1 a').css('position') == 'fixed') {
			var ribbonPosition = 'absolute';
			var ribbonTitle = 'Klicken, um das Logo mitscrollen zu lassen';
		} else {
			var ribbonPosition = 'fixed';
			var ribbonTitle = 'Klicken, um das Logo oben zu fixieren';
		}

		$('h1').empty();
		$('#tooltipHelper').remove();
		$('h1').html('<a href="javascript:void(0);"><span>Andreas Demmer</span></a>');

		$('h1 a').css('position', ribbonPosition);
		$('h1 a').attr('title', ribbonTitle);
		$('h1 a').click(changeRibbonPositon);
		$('h1 a').blur();

		$.post('/helpers/modifyRibbonPosition.php', {
			'position': ribbonPosition
		});

	}

	if ($('h1 a').css('position') == 'fixed') {
		var ribbonTitle = 'Klicken, um das Logo oben zu fixieren';
	} else {
		var ribbonTitle = 'Klicken, um das Logo mitscrollen zu lassen';
	}

	$('h1 a').click(changeRibbonPositon);
	$('h1 a').removeAttr('href');
	$('h1 a').attr('title', ribbonTitle);

	$('h1 a').ToolTip({
		className: 'tooltip',
		position: 'mouse',
		delay: 0
	});
}

/*
 * document ready function
 */
$(function () {
	initializeMenus();
	fixIE6alphaTransparency();
	initializeHeadlines();
	initializeSearchform();
	initializeRibbon();
	initializeImageboxes();
	initializeToTopLink();
});